// this file is used to handle the slice of userId
import { PayloadAction, createSlice } from '@reduxjs/toolkit';

interface UserState {
  userId: string | null;
  member: Member.MemberInfo | null;
  isLogin: boolean; // 是否登录
  subscribe: boolean;
  info: Member.MemberInfo | null;
}

const initialState: UserState = {
  userId: null,
  // 订阅状态: ,
  subscribe: false,
  member: null,
  isLogin: false,
  info: null,
};

export const userSlice = createSlice({
  name: 'user',
  initialState: initialState,
  reducers: {
    setUserId: (state, action: PayloadAction<string>) => {
      state.userId = action.payload;
    },
    setInfo(state, action: PayloadAction<Member.MemberInfo | null>) {
      if (action.payload && action.payload.status && [3, 4, '3', '4'].includes(action.payload.status)) {
        state.subscribe = false;
      } else {
        state.subscribe = true;
      }

      state.member = action.payload;

      state.info = action.payload;
    },
    // 设置登录状态
    setLogin: (state, action: PayloadAction<boolean>) => {
      state.isLogin = action.payload;
    },
    // 清除全部数据
    clearAll: (state) => {
      state.member = null;
      state.isLogin = false;
      state.subscribe = false;
      state.userId = null;
    },
  },
});

export const { clearAll, setInfo, setUserId, setLogin } = userSlice.actions;
